package com.yanqu.road.server.cmd.cookboy.process2;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.cookboy.process2.CookBoy2Spoon;
import com.yanqu.road.pb.cookboy.CookBoyProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.cookboy.CookBoyModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.activity.cookboy.CookBoyActivityMgr;
import com.yanqu.road.server.manger.activity.cookboy.CookBoyMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.manger.activity.cookboy.OtherActivityBoyActivityMgr;
import com.yanqu.road.server.manger.activity.cookboy.OtherActivityCookBoy2;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.PropertyHelper;

@Cmd(code = Protocol.S_COOK_BOY2_JOIN_TABLE, desc = "参加宴会")
public class CookBoy2JoinTableCmd extends AbstractCommand {

	//本服的活动
	int activityId;
	long homeUserId;
	int tableId;
	int spoonId;
	//可赴宴的活动
	int otherActivityId;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		CookBoyProto.CookBoy2JoinTableReqMsg reqMsg = CookBoyProto.CookBoy2JoinTableReqMsg.parseFrom(packet.getBytes());
		activityId = reqMsg.getActivityId();
		homeUserId = reqMsg.getUserId();
		tableId = reqMsg.getTableId();
		spoonId = reqMsg.getSpoonId();
		if(reqMsg.hasOtherActivityId()){
			otherActivityId = reqMsg.getOtherActivityId();
		}
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		ActivityInfo activityInfo = CookBoyActivityMgr.getInTimeActivityInfo(activityId);
		if(activityInfo == null){
			ret = GameErrorCode.E_RELATION_ACTIVITY_NO_IN_TIME;
			return;
		}
		if(!CookBoyActivityMgr.isActivityMatchTime(activityInfo)){
			ret = GameErrorCode.E_COOK_BOY_NOT_IN_MATCH_TIME;
			return;
		}
		CookBoy2Spoon cookBoy2Spoon = CookBoyActivityMgr.getCookBoy2Spoon(activityId,spoonId);
		if(cookBoy2Spoon == null){
			ret = GameErrorCode.E_COOK_BOY2_SPOON_NOT_FOUND;
			return;
		}
		if(!player.getModule(CookBoyModule.class).isAble(activityId)){
            ret = GameErrorCode.E_COOK_BOY2_USER_NOT_ABLE;
            return;
        }
		if(player.getModule(CurrencyModule.class).currencyIsEnough(PropertyHelper.parseStringToProperty(cookBoy2Spoon.getConsume()))){
			player.getModule(PreReduceModule.class).preReduce(cookBoy2Spoon.getConsume());
		}else{
			ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
			return;
		}
		if(otherActivityId > 0 && otherActivityId != activityId){
			//赴宴范围的试吃
			OtherActivityCookBoy2 otherActivityCookBoy2 = OtherActivityBoyActivityMgr.getOtherActivityCookBoy2(otherActivityId);
			if(otherActivityCookBoy2 != null){
				CookBoyMgr.getLogger().info("玩家{}，试吃赴宴范围活动 {}，玩家{}， 桌子{}，菜品 {}", player.getUserId(), otherActivityId, homeUserId, tableId, spoonId);
				ret = otherActivityCookBoy2.sendJoinMsg(player.getUserId(),homeUserId,tableId,spoonId, activityId);
				if(ret == 0){
					//赴宴范围的场景
					otherActivityCookBoy2.addSceneUser(player.getUserId(), homeUserId);
				}
			}
		}else {
			ret = CookBoyMgr.sendJoinMsg(activityId,player.getUserId(),homeUserId,tableId,spoonId, activityId);
			CookBoyMgr.addCookBoy2SceneUser(activityId, player.getUserId(), homeUserId);
		}
	}

	@Override
	public void sendData(GamePlayer player) {
		if(ret != 0){
			CookBoyProto.CookBoy2JoinTableRespMsg.Builder respMsg = CookBoyProto.CookBoy2JoinTableRespMsg.newBuilder();
			respMsg.addAllForbidSpoonId(player.getModule(CookBoyModule.class).getForbidSpoonIdList(activityId));
			respMsg.setRet(ret);
			player.sendPacket(Protocol.U_COOK_BOY2_JOIN_TABLE, respMsg);
		}
	}
}
